/**
 * Created by sa on 2016/9/22.
 */
"use strict";
var fs = require('fs')
var page = require('webpage').create(),
  system = require('system'),
  address;

//if (system.args.length === 1) {
//  console.log('Usage: netlog.js <some URL>');
//  phantom.exit(1);
//} else {
//  address = system.args[1];
  var txtPath = 'D:/work_nodeJS/crawl_youku_nodejs/spider/' + new Date().valueOf() + '.txt';
  page.onResourceRequested = function (req) {
    var context = JSON.stringify(req, undefined, 4);
    console.log('requested: ' + context);
    try {
      fs.write(txtPath, "--requested--", 'a+');
      fs.write(txtPath, context, 'a+');
    } catch(e) {
      console.log(e);
    }
  };

  page.onResourceReceived = function (res) {
    var context = JSON.stringify(res, undefined, 4);
    console.log('received: ' + context);
    try {
      fs.write(txtPath, "--received--", 'a+');
      fs.write(txtPath, context, 'a+');
    } catch(e) {
      console.log(e);
    }
  };

  page.open('http://wj.qq.com', function (status) {
    if (status !== 'success') {
      console.log('FAIL to load the address');
    }
    phantom.exit();
  });
//}
